#ifndef FLOW_MANAGER_H
#define FLOW_MANAGER_H

#include <list>
#include <mutex>
#include <unordered_map>
#include "ringflow.h"

#define FLOW_ERROR_BASE_CODE   (-1000)
#define FLOW_BUF_CREATE_FAILED (FLOW_ERROR_BASE_CODE - 1)
#define FLOW_BUF_NOT_FOUND     (FLOW_ERROR_BASE_CODE - 1)

typedef struct flow_node
{
    rflow_t *flow;
    int buf_size;
    int buf_count;
    int flow_id;
    int last_read_post;
    int flow_counter;
    char media_id[33];
} flow_node;

class FlowManager
{
public:
    static FlowManager *getInstance();
    ~FlowManager();

    int CreateFlowId(int size, int count);
    void DestoryFlowId(int flow_id);
    int IsvalidFlowId(int flow_id);

    int WriteDataToFlow(int flow_id, char *data, int size);
    int WriteDataToFlow(rflow_t *flow, char *data, int size);
    int ReadDataFromFlow(int flow_id, char *data, int size, int *post);
    char* GetMediaId(int flow_id);

private:
    FlowManager();
    rflow_t *GetRflowByFlowId(int flow_id);

private:
    static FlowManager *mInstance;
    static std::mutex mInstanceMutex;
    std::unordered_map<int, flow_node> mUMap;
    int mFlowCounter;
};

int CreateBufferFlowId(int size, int count);
int WriteDataToBufferFlow(int flow_id, char *data, int size);
int ReadDataFromBufferFlow(int flow_id, char *data, int size, int* post);
void FreeBufferFlowId(int flow_id);

#endif